package com.andatsoft.laisim.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.entity.Song;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.ToastMessage;

public class EditTagInfoActivity extends BaseActivity {

	// Views
	private TextView mBtnSave, mBtnCancel;
	private EditText mEtTtile, mEtArtist, mEtAlbum, mEtGenre;

	// Vars
	private int mSongId;
	private Song mSong;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_tags);

		mSongId = getIntent().getIntExtra(
				MainActivity.INTENT_DATA_MAIN_EDIT_TAGS_SONG_ID, -1);

		if (mSongId == -1)
			return;
		initViews();
		new SongInfoLoader().execute(0);
		setupListeners();
	}

	private void initViews() {
		mBtnSave = (TextView) findViewById(R.id.tv_edit_tags_save);
		setBackgroundColorForView(mBtnSave);
		mBtnSave.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		mBtnCancel = (TextView) findViewById(R.id.tv_edit_tags_cancel);
		setBackgroundColorForView(mBtnCancel);
		mBtnCancel.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());

		mEtTtile = (EditText) findViewById(R.id.et_edt_tags_title);
		mEtAlbum = (EditText) findViewById(R.id.et_edt_tags_album);
		mEtArtist = (EditText) findViewById(R.id.et_edt_tags_artist);
		mEtGenre = (EditText) findViewById(R.id.et_edt_tags_genre);

	}

	private void setupListeners() {

		mBtnSave.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					// save data
					if (mSong == null)
						return true;
					mSong.setTitle(mEtTtile.getText().toString());
					mSong.setAlbum(mEtAlbum.getText().toString());
					mSong.setArtist(mEtArtist.getText().toString());
					mSong.setGenre(mEtGenre.getText().toString());
					new SongInfoLoader().execute(1);
					return true;
				}
				return false;
			}
		});

		mBtnCancel.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					finishWork();
					return true;
				}
				return false;
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		applyTheme();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finishWork();
	}

	private void finishWork() {
		Intent i = new Intent();
		i.putExtra(MainActivity.INTENT_DATA_MAIN_EDIT_TAGS_SONG_ID, mSongId);
		EditTagInfoActivity.this.setResult(Common.RESULT_CODE_EDIT_TAGS, i);
		EditTagInfoActivity.this.finish();
		overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
	}

	class SongInfoLoader extends AsyncTask<Integer, Void, Void> {
		private int mCommand;

		@Override
		protected Void doInBackground(Integer... params) {
			mCommand = params[0];
			DataBaseAccess db = DataBaseAccess
					.getInstance(getApplicationContext());
			if (mCommand == 0) {

				mSong = db.getSong(mSongId);
			} else // save
			{
				db.updateSong(mSong);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void re) {
			if (mCommand == 0) {
				if (mSong == null)
					return;
				mEtTtile.setText(mSong.getTitle());
				mEtAlbum.setText(mSong.getAlbum());
				mEtArtist.setText(mSong.getArtist());
				mEtGenre.setText(mSong.getGenre());
			} else {
				new ToastMessage(getApplicationContext())
						.showShortMessage(getString(R.string.toast_saved));
				finishWork();
			}

		}

	}

	private void applyTheme() {
		Theme theme = ThemeManager.getSelectedTheme();
		
		applyThemeForEditText(mEtTtile);
		applyThemeForEditText(mEtAlbum);
		applyThemeForEditText(mEtArtist);
		applyThemeForEditText(mEtGenre);
		((TextView) findViewById(R.id.tv_edit_tags_title)).setTextColor(theme
				.getNormalTextColor());
		((TextView) findViewById(R.id.tv_edit_tags_album)).setTextColor(theme
				.getNormalTextColor());
		((TextView) findViewById(R.id.tv_edit_tags_artist)).setTextColor(theme
				.getNormalTextColor());
		((TextView) findViewById(R.id.tv_edit_tags_genre)).setTextColor(theme
				.getNormalTextColor());
	}
	
	private void applyThemeForEditText(EditText et)
	{
		et.setBackgroundColor(getSelectedTheme()
				.getDialogTextColor());
		et.setTextColor(getSelectedTheme()
				.getDialogBackgroundColor() | 0xff000000);
	}
}
